﻿using AllInOne.Classes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace AllInOne.MainPages
{
    /// <summary>
    /// Confirm.xaml 的交互逻辑
    /// </summary>
    public partial class Confirm : Window
    {
        DialogViewModel viewModel;
        public Confirm(string showMsg,int countDown)
        {
            InitializeComponent();
            viewModel = new DialogViewModel() { ShowMsg = showMsg, CountDown = countDown };
            DataContext = viewModel;
        }

        readonly CancellationTokenSource tokenSource = new CancellationTokenSource();
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            SimpleConsole.WriteQuitLog("在弹出框里，退出倒计时开始", this);
            Cpb.Maximum = viewModel.CountDown;
            CancellationToken token = tokenSource.Token;

            Task countTask = new Task(() =>
            {
                while (true)
                {
                    if (!token.IsCancellationRequested)
                    {
                        viewModel.CountDown--;
                        if (viewModel.CountDown == 0)
                            QuitView(false);
                        SimpleConsole.WriteQuitLog($"在弹出框里，退出倒计时{viewModel.CountDown}秒", this);
                        Thread.Sleep(1000);
                    }
                    else
                        return;
                }
            }, token);
            countTask.Start();
        }
        private void QuitView(bool result)
        {
            this.Dispatcher.Invoke(new Action(() =>
            {
                tokenSource.Cancel();
                this.DialogResult = result;
                this.Close();
            }));
        }
        private void BdQuit_Click(object sender, RoutedEventArgs e)
        {
            SimpleConsole.WriteQuitLog($"在弹出框里，点击了确认取消", this);
            QuitView(false);
        }

        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            SimpleConsole.WriteQuitLog($"在弹出框里，点击了确认按钮【确认退出】", this);

            QuitView(true);
        }
    }
}
